package chapter07;

import java.security.PublicKey;
import java.util.ArrayList;
import java.util.Scanner;

public class KTVSystem  {

            private ArrayList<String> songList = new ArrayList<>();

            public static void main(String[] args) {
                KTV system = new KTV();
                system.start();
            }

            public void start() {
                Scanner scanner = new Scanner(System.in);
                
                System.out.println("------欢迎来到点歌系统------");
                System.out.println("1.添加歌曲至列表");
                System.out.println("2.将歌曲置顶");
                System.out.println("3.将歌曲前移一位");
                System.out.println("4.退出");
                System.out.println("5.查看歌曲列表");
                System.out.println("6.删除歌曲信息");

                int choice;
                while (true) {
                    System.out.print("请输入指令(1-6): ");
                    choice = scanner.nextInt();
                    scanner.nextLine(); // 消耗掉换行符

                    switch (choice) {
                        case 1:
                            this.addSong(scanner);
                            break;
                        case 2:
                            this.moveSongToTop(scanner);
                            break;
                        case 3:
                            this.moveSongUp(scanner);
                            break;
                        case 4:
                            System.out.println("退出系统。");
                            return;
                        case 5:
                            this.showSongs();
                            break;
                        case 6:
                            this.deleteSong(scanner);
                            break;
                        default:
                            System.out.println("无效的指令，请重新输入。");
                    }
                }
            }

            private void addSong(Scanner scanner) {
                System.out.print("请输入歌曲名: ");
                String songName = scanner.nextLine();
                songList.add(songName);
                System.out.println("当前歌曲列表: " + songList);
            }

            private void moveSongToTop(Scanner scanner) {
                System.out.print("请输入要置顶的歌曲名: ");
                String songName = scanner.nextLine();
                int index = songList.indexOf(songName);
                if (index != -1) {
                    songList.remove(index);
                    songList.add(0, songName);



                    System.out.println("歌曲已置顶，当前歌曲列表: " + songList);
                } else {
                    System.out.println("歌曲不存在，无法置顶。");
                }
            }

            private void moveSongUp(Scanner scanner) {
                System.out.print("请输入要前移的歌曲名: ");
                String songName = scanner.nextLine();
                int index = songList.indexOf(songName);
                if (index != -1 && index > 0) {
                    songList.remove(index);
                    songList.add(index - 1, songName);
                    System.out.println("歌曲已前移，当前歌曲列表: " + songList);
                } else {
                    System.out.println("歌曲不存在或已在首位，无需前移。");
                }
            }

            private void showSongs() {
                System.out.println("当前歌曲列表: " + songList);
            }

            private void deleteSong(Scanner scanner) {
                System.out.print("请输入要删除的歌曲名: ");
                String songName = scanner.nextLine();
                int index = songList.indexOf(songName);
                if (index != -1) {
                    songList.remove(index);
                    System.out.println("歌曲已删除，当前歌曲列表: " + songList);
                } else {
                    System.out.println("歌曲不存在，无法删除。");
                }
            }
        }
